package com.cwj;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wujie.chen
 * 2021/11/3 15:26
 */
public class CreateTreeTest2 {
    private static Map<Integer, Node> trees = new HashMap<>();
    private static List<Role> data = new ArrayList<>();

    static {
        data.add(new Role(1, 0, "1"));
        data.add(new Role(2, 0, "2"));
        data.add(new Role(3, 1, "3"));
        data.add(new Role(4, 1, "4"));
        data.add(new Role(5, 2, "5"));
        data.add(new Role(6, 2, "6"));
        data.add(new Role(7, 2, "7"));
        data.add(new Role(8, 3, "8"));
        data.add(new Role(9, 4, "9"));
        data.add(new Role(10, 5, "10"));
        data.add(new Role(11, 6, "11"));
        data.add(new Role(12, 7, "12"));

    }

    /**
     * 1
     *      3
     *          8
     *      4
     *          9
     * 2
     *      5
     *          10
     *      6
     *          11
     *      7
     *          12
     *
     * <p>
     * 2 --
     * 5,6, 7
     *
     * @param args
     */
    public static void main(String[] args) {

        for (Role role : data) {
            if (role.getPid().equals(0)) {
                List<Node> childrenNode = getChildrenNode(role, 0);
                trees.put(role.getId(), new Node(role.getName(), 0, childrenNode));
            }
        }
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            System.out.println(objectMapper.writeValueAsString(trees));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    private static List<Node> getChildrenNode(Role role, int n) {
        List<Node> list = new ArrayList<>();
        for (Role e : data) {
            if (role.id.equals(e.pid)) {
                Node node = new Node(e.name, n + 1, getChildrenNode(e, n + 1));
                list.add(node);
            }
        }
        return list;
    }

    public static class Node {
        private String level1;
        private String level2;
        private String level3;
        private List<Node> list;

        public Node() {
        }

        public Node(String level, int n, List<Node> list) {
            switch (n) {
                case 0:
                    this.level1 = level;
                    break;
                case 1:
                    this.level2 = level;
                    break;
                case 2:
                    this.level3 = level;
                    break;
                default:
                    throw new RuntimeException("level异常");
            }
            this.list = list;
        }

        public String getLevel1() {
            return level1;
        }

        public void setLevel1(String level1) {
            this.level1 = level1;
        }

        public String getLevel2() {
            return level2;
        }

        public void setLevel2(String level2) {
            this.level2 = level2;
        }

        public String getLevel3() {
            return level3;
        }

        public void setLevel3(String level3) {
            this.level3 = level3;
        }

        public List<Node> getList() {
            return list;
        }

        public void setList(List<Node> list) {
            this.list = list;
        }

        @Override
        public String toString() {
            return "Node{" + "level1='" + level1 + '\'' + ", level2='" + level2 + '\'' + ", level3='" + level3 + '\''
                + ", list=" + list + '}';
        }
    }

    public static class Role {
        public Role(Integer id, Integer pid, String name) {
            this.id = id;
            this.pid = pid;
            this.name = name;

        }

        private Integer id;
        private Integer pid;
        private String name;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public Integer getPid() {
            return pid;
        }

        public void setPid(Integer pid) {
            this.pid = pid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}


